<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getHelpCenterList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'help_center_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')">
          新增系统单页
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'help_center_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'help_center_del'" type="danger" link :icon="Delete" @click="deleteFriendLink(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <HelpCenterEdit ref="drawerRef" />
  </div>
</template>
<script setup lang="tsx" name="helpCenter">
import { ref, reactive } from "vue";
import { Cms } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import HelpCenterEdit from "@/views/cms/helpcenter/HelpCenterEdit.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getHelpCenterList, saveHelpCenter, delHelpCenter } from "@/api/modules/cms";
import { useHandleData } from "@/hooks/useHandleData";
// ProTable 实例
const proTable = ref<ProTableInstance>();

// 表格配置项
const columns = reactive<ColumnProps<Cms.HelpCenter>[]>([
  { prop: "name", label: "名称" },
  { prop: "code", label: "标识" },
  { prop: "createdAt", label: "创建时间" },
  { prop: "updatedAt", label: "更新时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
]);

// 删除权限资源
const deleteFriendLink = async (params: Cms.HelpCenter) => {
  await useHandleData(delHelpCenter, { ids: params.id }, `删除【${params.name}】友情链接`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof HelpCenterEdit> | null>(null);
const openDrawer = (title: string, row: Partial<Cms.HelpCenter> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveHelpCenter,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
